<?php

namespace app\common\controller;

use think\Controller; 
class AliPay extends Controller
{
	 /*
    配置参数
    */
    private $config = array(
        'appId' => "2021001168663709",   /*支付宝AppId*/
        'rsaPrivateKey' => "MIIEpQIBAAKCAQEAhTguANzQutKqDPON/MGk1OZVj0NpeC7984CDdt3Sp0UD71ECMtwQLFEKpvTaws/qnaP3JPjumOKwk8B3P42XbMkMqe4tBNvjSfXeE++ajMMrrkeczIYB4d9EG1uAxDE/DxCGyie3vbLIGlCHAcQh91CdmHvYnLShQi3V39X/d4WcpNsmBsrsnxzBjXHj1m+xOccAImzlf/4DpEw2whfELli5xX6v9rkOrJ3jrq0dUiASayjaALhAVcy/q97L6y8bqQXw54yqMXYHGzTYwdefATxyb7vq8QA4ZOiFgABNlrio8i8rkKFKVjosQ+/A+JeaW4UisVRAVmtX7R37S697HwIDAQABAoIBAQCEltyaeSu5tXAQQdvAg86VIsZnNxXiywpv7ylpBe/iETuQVZUVn5btNJ+gebS9DgiZaxbQkn2OZpiOBZHqC/d5C0TyYB4oZt15wB6OwvuUO6/e3mF94aY/82GqxNCM8+KqEK3Gx+8E+8pS/nDQXi/cIT0pBaIanWCeKFZkWG3RuxbdZhwtWcv02fbaENYeWwC9o8BJxAS/0seOJByR22VdhmZMs0sVN3TB/63D91yozYMfqqWPH4o7nt7lz7baq85v9KjBSskcMbctpwWygW2x+sjE2oR2FEQ+FDWg8MOCWZilvYiCO6kysL2bkD4ySlWCQC2C2n6o3nkmB+RmlnMBAoGBAL5e1Ydd+rNcBA1zks1miYrvgjs20LS4oyDAV1/Oh5tg7r+II5Lvl8yjr+n90Z3hcFtpbYcLBbqcfh7hy5A81nyXhaAhoiKYWI/pmvg7xXjcdh84FMClcKbVjLRkd+6CNnG9Ro1g6FEL84hZBjIeS8FAbtoCpJ8sxrUQb0hrPeafAoGBALMlelz/GGURUrRoNfUbeiDPAoAA6Jd7esYAKjisjHoiqhzUAVvohMUsw8sC3lxFEeWWx3VheYOOh+Rj/Wap14YipOk7/YIagNC9UVGYBbDn21DOGnoqXsdY76zepHuPxeTmOXaFE9SGjHPiobZfdw515zcbcOTtEmRSsRlXpJuBAoGBAKR2dW5u6GkPc4jTcd31WZon9JCHeKLIaFWhFVr1p/26LkhVqrYEO1ZVi2JRH0iQPGscl7z07swa6nJvi1de18XneU+v/sx4ZPpfTLxrltwlSf8KihMsWjgngVlO+rWY1MRKSqtVLx7lfk0q68lclwGrUpkRc3r4Seqe+iTMUTvtAoGAMF3oAX+l+HOK3sZb+wcmHAGH7UNRiMR/y0F0JMUBUGBXiVz72PKYFhwV6JOnsUW8pT3mMBzXD9vFeLY+Th+A3ugvmgDmWuAI81KJx/vBXZfbsJ9UqrMRz8SFQSkDQ5D3nutBjGw1hGWnOgpwGqr/m9ndSRWRj10qYAerZOoyuQECgYEAovO5bitdyQnmRR2ZbXuoGgYpaC6vAOQ5MYoFr7CAxCLAki4R797Vz/j+8kgVwhho7vyVISkFM/VOTT7BPVwuikgm0edhG0aKwFmq+CHsCnTz28vrZx+zw0BDG7s07gf9F2Ibj8YRj9H/ELmdqTWe9xx1ipafaSp8iXkXH+13yfk=",   /*支付宝私钥*/
        'aliPayRsaPublicKey' => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr/u+YMMl4rKaDMuKa0QSg37Wk4ZcDoXz9I+v4MSYELwVLfu3CLmNWRrzH+6k2ivRaB0ZQl9e97Fud9MJTPJewYNC+LNTRhobW6DEm+H2Ffe4V1J9eSOs2giz0EAhKVggrqGsIUit1gmiuFPzi/9h6zsamOqFQ6zcNHwB19PORJDEIYAJ1ucleQwMUklfD4EttXXYQUqm94CTqbQJZwy9cr8J3buQ60N0S0cyPoHkw+jAJL8ZPkpDcYGOcDfiq/Op0bfIqYVQkJF1jFniDNNqfu68uq3BhxAeJRq0KuUgyNdTC8JAvV6rOsQ2cjlmspANfqQCf2bjCYnHEBUZFDbHGwIDAQAB"    /*支付宝公钥*/
    );

	public function aliPay($body,$total_fee,$out_trade_no,$notify_url)
	{
        import('Alipay.AopClient',EXTEND_PATH);
        import('Alipay.AlipayTradeAppPayRequest',EXTEND_PATH);
        $aop = new \AopClient();
		$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = $this->config['appId'];
        $aop->rsaPrivateKey = $this->config['rsaPrivateKey'];
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        $aop->alipayrsaPublicKey = $this->config['aliPayRsaPublicKey'];
        $request = new \AlipayTradeAppPayRequest();
        $arr['subject'] = $body;
        $arr['out_trade_no'] = $out_trade_no;
        $arr['timeout_express'] = '30m';
        $arr['total_amount'] = floatval($total_fee);
        $arr['product_code'] = 'QUICK_MSECURITY_PAY';
        $json = json_encode($arr);
        $request->setNotifyUrl($notify_url);
        $request->setBizContent($json);
        $response = $aop->sdkExecute($request);
        return $response;
        
	}

    public function aliRefund($body,$refund_amount,$out_trade_no)
    {
        // $refund_amount = '0.01';
        import('Alipay.AopClient',EXTEND_PATH);
        import('Alipay.AlipayTradeRefundRequest',EXTEND_PATH);
        $aop = new \AopClient();
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = $this->config['appId'];
        $aop->rsaPrivateKey = $this->config['rsaPrivateKey'];
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        $aop->alipayrsaPublicKey = $this->config['aliPayRsaPublicKey'];
        $request = new \AlipayTradeRefundRequest();
        $arr['subject'] = $body;
        $arr['out_trade_no'] = $out_trade_no;
        $arr['refund_amount'] = $refund_amount;
        $arr['refund_reason'] = '正常退款';
        $json = json_encode($arr);
        $request->setBizContent($json);
        // $request->setBizContent("{" .
        //    "\"trade_no\":\"2017112821001004030523090753\"," .
        //     "\"out_trade_no\":\"$out_trade_no\"," .
        //     "\"refund_amount\":$refund_amount," .
        //     "\"refund_reason\":\"正常退款\"" .
        //     "  }");
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            return array('code'=> 200,'msg'=>'退款成功');
        }else{
            $data = array();
            $data['return_info'] = $result->$responseNode->sub_msg;
            return array('code'=>201,'msg'=>'退款失败','data'=>$data);
        }
    }

}